package com.example.demo.practice.tool;


import android.os.Handler;

/**
 * 定时操作
 */
public class TimeAction {
    private boolean isWork;
    private Handler handler;
    private int time;
    public TimeAction(Handler handler,  int time) {
        this.handler=handler;
        this.time=time;
        isWork=true;
    }

    /**
     * 启动定时任务
     */
    public void start(){
        Thread thread=new Thread(new Runnable() {
            @Override
            public void run() {
                while (isWork) {
                    try {
                        Thread.sleep(time);// 线程暂停10秒，单位毫秒
                        handler.sendEmptyMessage(0);// 发送消息
                    } catch (InterruptedException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
            }
        });
        thread.start();
    }

    public void stop(){
        isWork=false;
    }


}
